﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

public partial class Control_complexListBox : controlMatrix //System.Web.UI.UserControl
{
    //[Description("設定顯示名"), Category("MISAPP"), DefaultValue("設定顯示名")]
    //public string Caption
    //{
    //    set;
    //    get;
    //}

    [Description("LisbBox 中的 Row"), Category("MISAPP"), DefaultValue(4)]
    public int Rows
    {
        set
        {
            ListBox1.Rows = value;
        }
        get
        {
            return ListBox1.Rows;
        }
    }

    [Description("LisbBox 中的 SelectionMode"), Category("MISAPP"), DefaultValue(4)]
    public ListSelectionMode SetSelectionMode
    {
        set
        {
            if ((value < ListSelectionMode.Single) || (value > ListSelectionMode.Multiple))
            {
                throw new ArgumentOutOfRangeException("value");
            }
            ListBox1.SelectionMode = value;
        }

    }

    protected bool showCheckBox = true;
    [Description("是否顯示出CheckBox"), Category("MISAPP"), DefaultValue(true)]
    public bool ShowCheckBox
    {
        set
        {
            if (ViewState["showCheckBox"] == null)
            { 
                ViewState["showCheckBox"] = value;
            }
            showCheckBox = value;
        }
        get
        {
            if (ViewState["showCheckBox"] != null)
            {
                return (bool)ViewState["showCheckBox"];
            }
            return showCheckBox;
        }
    }

    [Description("這一個欄位是否要當成GroupBy運算"), Category("MISAPP"), DefaultValue(true)]
    public bool ShoudGroupBy
    {
        set
        {
            if (this.ShoudGroupBy != value)
            {
                this.ViewState["ShoudGroupBy"] = value;
            }
        }
        get
        {
            object obj2 = this.ViewState["ShoudGroupBy"];
            if (obj2 != null)
            {
                return (bool)obj2;
            }
            else
            {
                object obj = Utility.GetAttributeDefaultValue("ShoudGroupBy", this);
                return (bool)obj;
            }
        }       
    }

    

    protected void Page_Load(object sender, EventArgs e)
    {
        if (ShowCheckBox == false)
        {
            chk_NotIn.Visible = ShowCheckBox;

            Utility.LoadJquery(this, "jquery", "jquery");
            Utility.LoadJquery(this, "jqueryUI", "jqueryUI");
           
            string xx = "$('#" + ClientID + "').css('visibility', 'hidden');";
            string script = " $(function(){" + xx + " });";
                            
            System.Web.UI.ScriptManager.RegisterClientScriptBlock(
                    this
                    , this.GetType()
                    , ClientID
                    , script
                    , true);        
        }
    }

    
    public void SetData(System.Data.DataTable p_dt, string p_TextField,string p_ValueField)
    {
        ListBox1.DataTextField = p_TextField;
        ListBox1.DataValueField = p_ValueField;
        ListBox1.DataSource = p_dt;
        ListBox1.DataBind();
    }

    public void SetData(System.Data.DataTable p_dt, int p_TextFieldPOS, int p_ValueFieldPOS)
    {
        if (p_dt.Columns.Count == 1)
        {
            p_ValueFieldPOS = 0;
        }
        SetData(p_dt
                , p_dt.Columns[p_TextFieldPOS].ColumnName
                , p_dt.Columns[p_ValueFieldPOS].ColumnName);
    }

    /// <summary>
    /// 陣列的第一個位置，放是否已勾選反向
    /// </summary>
    /// <returns></returns>
    public List<string> GetSelectList()
    {
        List<string> selects = new List<string>();
        string isChk = "NO";
        isChk = chk_NotIn.Checked ? "YES" : isChk;
        selects.Add(isChk);
        foreach (ListItem itm in ListBox1.Items)
        {
            if (itm.Selected)
            {
                selects.Add(itm.Value);
            }
        }
        if (selects.Count == 1) //如果等於1，表示沒有選。1是checkbox中的值
        {
            selects.Clear();
        }

        return selects;
    }

    /// <summary>
    /// 取回所選的單一值
    /// </summary>
    /// <returns></returns>
    public string GetSelectValue()
    {
        string sel = string.Empty;
        foreach (ListItem itm in ListBox1.Items)
        {
            if (itm.Selected)
            {
                sel = itm.Value;
                break;
            }
        }
        return sel;
    }

    public MISAPP.MISInterface.iSelectList GetSelectList(string p_field,object p_obj)
    {
        MISAPP.MISInterface.iSelectList sl = (MISAPP.MISInterface.iSelectList)p_obj;
        sl.SetReverse(chk_NotIn.Checked ? true : false);
        sl.SetField(p_field);
        sl.SetGroupBy(this.ShoudGroupBy);
        foreach (ListItem itm in ListBox1.Items)
        {
            if (itm.Selected)
            {
                sl.AddToList(itm.Value);
            }
        }
        return sl;        
    }
}
